import area from '@/data/area'

/**
 * 表格时间格式化
 */
export function formatDate(cellValue: any) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '-' + month + '-' + day
}

export const paramsStr = (data:any) => {
	let a = '?'
	for(let i in data) {
		a = a + i + '=' + data[i] + '&'
	}
	a = a === '?'?'':a.substring(0, Object.keys(a).length - 1)
	return a 
}

export const getDate = (datetime: any, type?: string) => {
	let date = ''
	if(type == 'stamp') {
		let year:any = datetime.getFullYear()
		let month:any =  datetime.getMonth() + 1 < 10 ? '0' + (datetime.getMonth() + 1) : datetime.getMonth() + 1
		let day:any = datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate()
		date = year + '-' + month + '-' + day
	} else {
		date = datetime.split(' ')[0]
	}
	
	const dateArr = date.split('-')
	if(type == 'month') {
		date = dateArr[0] + '-' + dateArr[1]
	}
	if(type == 'day') {
		date = dateArr[1] + '-' + dateArr[2]
	}
	return date
}

export const previewImg = (url: string) => {
	const domain = import.meta.env.VITE_APP_DOMAIN
	const baseApi = import.meta.env.VITE_APP_BASE_API
	return domain + baseApi + '/file/byUrl?url=' + url
}

// export const getImgSrc = (url: string) => {
// 	const domain = import.meta.env.VITE_APP_DOMAIN
// 	return domain + '/static'
// }
 
export const inputDigit = (value:string,digit:number) => {
  if(value.indexOf('.') > 0){
    return value=value.slice(0,value.indexOf('.') + digit + 1)
  }
  return value
}

export const setObjParams = (data:any) => {
	return encodeURIComponent(JSON.stringify(data))
}

export const getObjParams = (data:any) => {
	return JSON.parse(decodeURIComponent(data))
}

export const getArea = (provinceId:string,cityId:string,areaId:string) => {
	const areaArr:any = []
	for(let i in area) {
		if(area[i].value == parseInt(provinceId)) {
			areaArr.push(area[i].label)
			const arr = area[i].children
			for(let j in arr) {
				if(arr[j].value == parseInt(cityId)) {
					areaArr.push(arr[j].label)
					const arr1 = arr[j].children
					for(let z in arr1) {
						if(arr1[z].value == parseInt(areaId)) {
							areaArr.push(arr1[z].label)
						}
					}
				}
				
			}
		}
	}
	return areaArr
}


export const telphone = (phone:string) => {
	uni.makePhoneCall({
		// 手机号
		phoneNumber: phone,
		// 成功回调
		success: (res) => {
		},
		// 失败回调
		fail: (res) => {
		}
	})
	// 获取设备平台
	// let platform = uni.getSystemInfoSync().platform
	// console.log(platform)
	// switch (platform) {
	// 	case 'android':
	// 		uni.showActionSheet({
	// 		    itemList: [phone,'呼叫'],
	// 		    success:function(res){
	// 				console.log(res);
	// 				if(res.tapIndex==1){
	// 					plus.device.dial(phone,false)        
	// 				}
	// 		    },
	// 		    complete: function(res) {
	// 		       console.log("安卓失败",res)
	// 		    }
	// 		})
	// 		break;
	// 	case 'ios':
	// 		// 使用uni-app提供的借口
	// 		uni.makePhoneCall({
	// 			phoneNumber: phone
	// 		})
	// 		break;
	// 	default:
			
	// 	// 调试器工具
	//}
}

//获取两天之间的天数
export const  getDaysBetween = (startDate: string, endDate: string) => {
  const sDate = Date.parse(startDate)
  const eDate = Date.parse(endDate)
  if (sDate > eDate) {
    return -1
  }
  const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
  return days
}